<?php

require_once("../../class2.php");
require_once(HEADERF);
		$user_id = USERID;
		$user_name = USERNAME;
require_once("./includes/require_all.php");
echo '<script language="javascript" type="text/javascript" src="./junks/wz_tooltip.js"></script>';

function print_difficulty($chars){
	global $adminss;
	switch (TRUE) {
		case ($chars > 3000000 ):	$status="мисията невъзможна";	break;
		case ($chars > 2000000 ):	$status="гръм свисше";			break;
		case ($chars > 1000000 ):	$status="сложна работа";		break;
		case ($chars > 500000 ):	$status="препъни-камъче";		break;
		case ($chars > 200000 ):	$status="стандартна игра";		break;
		case ($chars > 150000 ):	$status="средна работа";		break;
		case ($chars > 100000 ):	$status="кратка игра";			break;
		case ($chars > 50000 ):		$status="залъгалка";			break;
		default: $status="дребосъче";
	}
	$mpages= ceil($chars/1800); $aslevas= ceil($mpages*7.5);
	$chars= number_format($chars, 0, ",", " ");
	if (in_array(USERNAME,$adminss)) $ttip= "Общо знаци: $chars\nКато маш.стр.: $mpages\nВ лева: $aslevas";
	return "<div title='$ttip' style='color:rgb(150,120,120);font-family:Courier,Arial,sans-serif;cursor:help;'>&lt;$status&gt;</div>";
}


//auto-update
$qqq = mysql_query("select max(date) from admin_panel where game_show = 1");
$aaa = mysql_fetch_array($qqq);
$post_time = $aaa[0];
$year = substr($post_time, 0, 4);
$month = substr($post_time, 5, 2);
$day = substr($post_time, 8, 2);
$date = mktime(00, 00, 01, $month, $day, $year);
$seconds = time() - $date;
//$days = floor($seconds/84600);
//echo date("d-m-Y H:i:s", time())." => ".date("d-m-Y H:i:s", $date);

if($seconds >= 84600){
	$q_update = mysql_query("select * from admin_panel where game_percent != '100'");
	while($a_update = mysql_fetch_array($q_update)){
		
		$gamename = $database->escape_value($a_update['game_name']);
		
		//Counting all rows for translation, all rows with cyrillic chars in them and percent
		$q_text = "SELECT * FROM ".$a_update['game_prefix']."_text WHERE content <> ''";
		$r_text = mysql_query($q_text);
		$all = mysql_num_rows($r_text); //Counter for all english rows for translation
		$all_cyr = 0; //Counter for all cyrillic rows that are already translated
		while($a_text = mysql_fetch_array($r_text)){
			$content = $a_text['content'];
			$text_id = $a_text['text_id'];
			if(preg_match("/[а-яА-Я]{2,}/u", $content) OR preg_match("/(_IGcoLLen){1}/", $text_id)){
				$all_cyr++;
			}
		}
		$percent = round(($all_cyr*100)/$all, 2);
		
		$translation_status = $database->fetch_array($database->query("select date, game_percent, increase from admin_panel where game_name = '$gamename'"));
		$perc = floatval(str_replace(",", ".", $translation_status['game_percent']));
		$p = $percent - $perc;
		$increase = floatval(str_replace(",", ".", $translation_status['increase']));
		$post_time = $translation_status['date'];
		$year = substr($post_time, 0, 4);
		$month = substr($post_time, 5, 2);
		$day = substr($post_time, 8, 2);
		$date = mktime(00, 00, 01, $month, $day, $year);
		$seconds = time() - $date;
		//$days = floor($seconds/84600);

		if($seconds < 84600){
			$increase += $p;
			$query = "UPDATE admin_panel SET rows_translated = $all_cyr, rows_all = $all, game_percent = '$percent', date = NOW(), increase = '$increase' WHERE game_name = '$gamename'";
			$database->query($query);
		}
		else{
			$query = "UPDATE admin_panel SET rows_translated = $all_cyr, rows_all = $all, game_percent = '$percent', date = NOW(), increase = '$p' WHERE game_name = '$gamename'";
			$database->query($query);
		}
	}
}
?>
<script>
	function insertFromURL(url,from,into) {
		$(into).load(url+' '+from);
	};
	function sendToURL(trans,url) {
		var text = $(trans).val();
		var taken = text.replace(/\\n/g, "✘");
		var answer = confirm(taken);
		if(answer) { $.post(url, { text: taken } ) };
	};
	function Nullify(tbc) {
		var area = document.getElementById (tbc);
		area.value = "";
	};
</script>
<?php

ramka("open");

echo "<h1 style='border-bottom:0px; padding-bottom:10px; margin-left:-1px;' class='forumheader4'>В процес на превод...</h1>";
$arr_in = mysql_query("SELECT * FROM admin_panel WHERE game_show = 1 AND game_prefix != 'ac2' ORDER BY game_platform ASC, game_name ASC");

	$delimiter= 2;
/*	echo "<div class='forumheader'><center>Новини</center></div>";
	$tempo= mysql_query("SELECT * FROM cms_gamenews ORDER BY date DESC LIMIT 0, 1");
	while ($neww= mysql_fetch_array($tempo)) {
		$datee= date("j.m", strtotime($neww['date']));
			echo "<div id='newsss'><div class='forumheader3'>
					<b style='font-size:14px'><u>$neww[game]</u></b> <span style='float:right;' class='forumheader2'>$datee ($neww[user])</span><br style='clear: both'>
					$neww[message] <div style='text-align:right;'><a style='cursor: pointer;' onclick=insertFromURL('intranslations-exec.php?limit=$delimiter&pager=1','#result','#newsss'); class='unselectable forumheader4'>Всички новини</a></div><div style='margin-top:-16px;'></div>
				  </div>";	  
	}
	echo "</div>";	*/

while ($row = mysql_fetch_array($arr_in)) {

	//-----СКРИВАНЕ-НА-ЧАСТНИТЕ-----\\
	IF($row["PRIVATEGAME"]==1){
		$row["game_name"]=  "Необявен превод ".$row["id"];
		$row["game_cover"]= "icons/stoper.png";
		$event= "alert('Достъпът е ограничен.')";
	}
	//------------------------------\\
	
	if ($row['charall']!=0 and in_array(USERNAME,$adminss)) {
		$gamenicked= print_difficulty($row['charall']);
		}
	else $gamenicked="";
	
	if ($row['game_percent']==100) {  
		$row['game_percent'] = "В процес на тестване";
		}
	else $row['game_percent'] = $row['game_percent']."%";

	IF ($user_name != "USERNAME") {
		$link= "href=\"$row[game_link]\"";
		}
	ELSE { $event= "alert('Работата по колективните преводи е достъпна само за регистрирани потребители.')"; }
	
	if ($row['level_access']==2 and in_array($user_name, $mods)) {
		IF($row["PRIVATEGAME"]!=0){ 
			$link= "href=\"$row[game_link]\"";
		}
	}
	elseif($row["PRIVATEGAME"]) {
		$link=null;
	}
	elseif ($event == null and $row['level_access']==2) {
		$link=null;
		$event= "alert('Избраният от вас работен превод е достъпен само за членове на екипа.')";
		}
	
	if ($temp == null OR $temp != $row['game_platform']) {
		if ($temp != null) {
			echo "</tr></table></div>";
		}
		$temp = $row['game_platform'];
		if ($temp == "PC") { 
			$icon = "http://store.picbg.net/pubpic/9A/E5/a080297acca89ae5.png"; 
			}
		elseif ($temp == "PS3") { 
			$icon = "http://store.picbg.net/pubpic/4B/34/111ebacc665d4b34.png";
			}
		elseif ($temp == "Dreamcast") { 
			$icon = "icons/dreamcast.png";
			}
		elseif ($temp == "Wii") { 
			$icon = "icons/Wii.png";
			}
		elseif ($temp == "Xbox360") { 
			$icon = "icons/Xbox360.png";
			}	
		echo '<div class="forumheader"><center style="padding-top: 5px;"><b><img src="'.$icon.'"></b></center></div>';
		$count = 0; $startTable = true;
	}
	if ($startTable === true) {
		$startTable = false;
		echo "<div class='forumheader3'><table WIDTH='100%'><tr>"; 
	}
	if ($count%3==0) { echo "</tr><tr>"; }

		if (in_array($user_name,$mods,true)) {
			$tempo= urlencode($row[game_name]);
			$addings1= "<span onclick=\"javascript:toggle('add_$row[game_prefix]')\"><img src='icons/edit1.png' style='cursor:pointer; float:right;'></span>";
			$addings2= "<span><textarea id='new_$row[game_prefix]' class='tbox chatbox' style='width:96%;'></textarea></span>";
			$addings3= "<button type='button' class='tbox' style='cursor:pointer;' onclick=sendToURL('#new_$row[game_prefix]','intranslations-exec2.php?pref=$row[game_prefix]&name=$tempo');Nullify('new_$row[game_prefix]');onclick=insertFromURL('intranslations-exec.php?limit=$delimiter&pager=1','#result','#newsss')>Изпрати</button>";
			if ($row[game_name] === 'Doctor Who: City Of The Daleks') {
				$addings1=null;$addings2=null;$addings3=null;
			}
		}
		
echo<<<HTML
		<td WIDTH='33%' style='vertical-align:top;'>
		<div id="$row[game_name]">
		<a alt="Превод на играта $row[game_name]" $link onmouseover="Tip('<img src=$row[game_cover] width=140 height=200px>')" onmouseout="UnTip()" onclick="$event"><img style="padding: 3px;" src="$row[game_cover]" align="left" height ="45" width="35">$row[game_name]</a><br>
		<span class="smalltext"><b>Прогрес:</b> 
HTML;
		if(strlen($row[game_percent]) > 10){
			echo $row[game_percent];
		}
		else{
			if(floatval(str_replace(",", ".", $row[increase])) != 0){
				echo $row[game_percent]." <span style='color:green; cursor: help;' title='изменение за деня'>(+ $row[increase]%)</span>";
			}
			else{
				echo $row[game_percent];
			}
		}
		if(in_array(USERNAME,$gods)) { $hellyaaaa= "add_$row[game_prefix]"; } else $hellyaaaa="";
echo<<<HTML
		</span> <span class="smalltext">  $addings1<br></span><span style="display: none" id="$hellyaaaa">$addings2<br><center>$addings3</center></span></div>$gamenicked
		</td>
HTML;
	$count++; $event=null;
}
echo "</tr></table></div>";
if (in_array($user_name,$mods,true)) { 
	echo "<div class='forumheader'><textarea id='gmessage' class='tbox' style='height:16px; width:50%;'>Глобален коментар</textarea><button type='button' class='tbox' style='vertical-align:top; cursor:pointer;' onclick=\"sendToURL('#gmessage','intranslations-exec2.php?name=%d0%93%d0%bb%d0%be%d0%b1%d0%b0%d0%bb%d0%bd%d0%be+%d1%81%d1%8a%d0%be%d0%b1%d1%89%d0%b5%d0%bd%d0%b8%d0%b5');Nullify('gmessage');onclick=insertFromURL('intranslations-exec.php?limit=$delimiter&pager=1','#result','#newsss')\">Изпрати</button>";
}

if ($user_name !== "USERNAME")
	{
		CMS_activitySave("intranslation.php");
	}

ramka("close");

if(USERNAME=="Mnementh") {
	$end_update= mysql_query("SELECT id,game_prefix,charall,charbg FROM admin_panel");
	WHILE ($tentry=mysql_fetch_assoc($end_update)){
		if($tentry['charall']==0){
			mysql_query("UPDATE admin_panel SET charall=(SELECT sum(char_length(english)) FROM {$tentry['game_prefix']}_text) WHERE id='{$tentry['id']}'");
		}
		mysql_query("UPDATE admin_panel SET charbg=(SELECT sum(char_length(content)) FROM {$tentry['game_prefix']}_text WHERE content!=english OR translator!='original' OR content REGEXP '[а-яА-Я ]{10}') WHERE id='{$tentry['id']}'");
		mysql_query("UPDATE admin_panel SET charall_translated=(SELECT sum(char_length(english)) FROM {$tentry['game_prefix']}_text WHERE content!=english OR translator!='original' OR content REGEXP '[а-яА-Я ]{10}') WHERE id='{$tentry['id']}'");
		$translators = mysql_query("SELECT translator as name, round(sum(char_length(content))/1800,1) as pages FROM {$tentry['game_prefix']}_text WHERE translator!='original' AND translator!='' GROUP BY translator ORDER BY translator ASC");
		WHILE ($translator=mysql_fetch_assoc($translators)){
			IF(isset($arr[$translator['name']])){$arr[$translator['name']]+=$translator['pages'];} ELSE $arr[$translator['name']]=$translator['pages'];
		}
	}
	FOREACH ($arr as $translator=>$pages){
		mysql_query("UPDATE cms_actusers SET machinni='$pages' WHERE user_name='$translator'");
	}
}

require_once(FOOTERF);

?>